// pages/garden-tips/garden-tips.js
Page({
  data: {
    tips: [
      {
        id: 1,
        title: '春季花卉养护要点',
        category: '养护技巧',
        image: '/images/care-tip1.jpg',
        summary: '春季是花卉生长的关键时期，掌握正确的养护方法能让植物更加茂盛。',
        author: '园艺师小李',
        publishTime: '2024-01-15',
        tags: ['春季', '养护', '花卉']
      },
      {
        id: 2,
        title: '多肉植物浇水技巧',
        category: '浇水管理',
        image: '/images/care-tip2.jpg',
        summary: '多肉植物浇水是一门学问，掌握正确的方法很重要。',
        author: '多肉达人小王',
        publishTime: '2024-01-10',
        tags: ['多肉', '浇水', '技巧']
      },
      {
        id: 3,
        title: '室内绿植摆放指南',
        category: '空间布局',
        image: '/images/care-tip1.jpg',
        summary: '合理的摆放位置能让绿植更好地生长，提升室内环境。',
        author: '室内园艺师',
        publishTime: '2024-01-08',
        tags: ['室内', '绿植', '摆放']
      },
      {
        id: 4,
        title: '花卉施肥完全指南',
        category: '施肥管理',
        image: '/images/care-tip4.jpg',
        summary: '正确的施肥能让花卉更加茂盛，掌握施肥技巧很重要。',
        author: '肥料专家',
        publishTime: '2024-01-05',
        tags: ['施肥', '花卉', '营养']
      },
      {
        id: 5,
        title: '植物病虫害防治',
        category: '病虫害',
        image: '/images/care-tip1.jpg',
        summary: '及时发现和处理植物病虫害，保护植物健康生长。',
        author: '植保专家',
        publishTime: '2024-01-03',
        tags: ['病虫害', '防治', '健康']
      },
      {
        id: 6,
        title: '园艺工具使用指南',
        category: '工具使用',
        image: '/images/care-tip6.jpg',
        summary: '选择合适的园艺工具，让园艺工作更加高效便捷。',
        author: '工具专家',
        publishTime: '2024-01-01',
        tags: ['工具', '使用', '效率']
      }
    ],
    categories: [
      { id: 'all', name: '全部', active: true },
      { id: '养护技巧', name: '养护技巧', active: false },
      { id: '浇水管理', name: '浇水管理', active: false },
      { id: '空间布局', name: '空间布局', active: false },
      { id: '施肥管理', name: '施肥管理', active: false },
      { id: '病虫害', name: '病虫害', active: false },
      { id: '工具使用', name: '工具使用', active: false }
    ],
    currentCategory: 'all',
    filteredTips: [],
    loading: false
  },

  onLoad() {
    this.setData({
      filteredTips: this.data.tips
    })
  },

  // 切换分类
  onCategoryChange(e) {
    const categoryId = e.currentTarget.dataset.id
    const categories = this.data.categories.map(item => ({
      ...item,
      active: item.id === categoryId
    }))
    
    this.setData({
      currentCategory: categoryId,
      categories: categories
    })
    
    this.filterTips()
  },

  // 筛选技巧
  filterTips() {
    let filteredTips = this.data.tips
    
    if (this.data.currentCategory !== 'all') {
      filteredTips = this.data.tips.filter(tip => tip.category === this.data.currentCategory)
    }
    
    this.setData({
      filteredTips: filteredTips
    })
  },

  // 点击技巧项
  onTipTap(e) {
    const tip = e.currentTarget.dataset.tip
    wx.navigateTo({
      url: `/pages/plant-care/plant-care?id=${tip.id}`
    })
  },


  // 搜索
  onSearchInput(e) {
    const keyword = e.detail.value
    if (keyword.trim() === '') {
      this.filterTips()
      return
    }
    
    const filteredTips = this.data.tips.filter(tip => 
      tip.title.includes(keyword) || 
      tip.summary.includes(keyword) ||
      tip.tags.some(tag => tag.includes(keyword))
    )
    
    this.setData({
      filteredTips: filteredTips
    })
  },

  // 清空搜索
  onClearSearch() {
    this.setData({
      searchKeyword: ''
    })
    this.filterTips()
  }
})
